package com.warm.base.design.s_flyweight;

import java.util.HashMap;

/**
 * @Date: 2019/11/22 13:49
 * @Author: LChuang
 * @Description:
 */
public class FlyweightFactory {

    /**
     * define container
     */
    private static HashMap<String, Flyweight> pool = new HashMap<>();

    /**
     * 享元工厂
     */
    public static Flyweight getFlyweight(String extrinsic){
        //需要返回对象
        Flyweight flyweight = null;
        // 在池中没有该对象就创建, 有就返回
        if(pool.containsKey(extrinsic)){
            flyweight = pool.get(extrinsic);
        }else {
            // 根据外部状态创建享元模式
            flyweight = new ConcreteFlyweight(extrinsic);
            // 放到池中
            pool.put(extrinsic, flyweight);
        }
        System.out.println("从池中取出对象");
        return flyweight;
    }
}
